/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer.gapvis.koichi;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import at.tugraz.kmi.css.analyzer.UserCompetence;

class CompetenceVertexDialog extends JDialog {
	private static final long						serialVersionUID	= 1L;
	private final UserCompetence					competence;
	private JLabel									labProficiency;
	private JLabel									labProficiencyValue;
	private JSlider									sldProficiency;
	private JLabel									labComment;
	private JTextArea								txtComment;
	private JButton									butOK;
	private JButton									butCancel;
	private final KiCurrentCompetenceProfilePane	owner;
	private static final int						MIN_PROFICIENCY		= 0;
	private static final int						MAX_PROFICIENCY		= 100;

	public CompetenceVertexDialog(JFrame frame, UserCompetence competence,
			KiCurrentCompetenceProfilePane owner) {
		super(frame, true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Edit properties: " + competence.toString());
		this.owner = owner;
		this.competence = competence;

		initializeComponents();

		labProficiencyValue.setText("" + competence.getProficiency());
		txtComment.setText(competence.getComment());
	}

	private void initializeComponents() {
		JPanel panProficiency = new JPanel(new BorderLayout());
		JPanel panProficiencyLabel = new JPanel();
		panProficiencyLabel.setLayout(new BoxLayout(panProficiencyLabel,
				BoxLayout.X_AXIS));
		labProficiency = new JLabel("Proficiency level: ");
		labProficiencyValue = new JLabel("100");
		sldProficiency = makeSlider();
		panProficiencyLabel.add(labProficiency);
		panProficiencyLabel.add(labProficiencyValue);
		panProficiency.add(panProficiencyLabel, BorderLayout.NORTH);
		panProficiency.add(sldProficiency, BorderLayout.CENTER);

		JPanel panComment = new JPanel();
		panComment.setLayout(new BorderLayout());
		labComment = new JLabel("Comments: ");
		txtComment = new JTextArea(5, 30);
		txtComment.setLineWrap(true);
		txtComment.setWrapStyleWord(true);
		JScrollPane scpComment = new JScrollPane(txtComment);
		panComment.add(labComment, BorderLayout.WEST);
		panComment.add(scpComment, BorderLayout.CENTER);

		JPanel panCtrl = new JPanel();
		panCtrl.setLayout(new BoxLayout(panCtrl, BoxLayout.X_AXIS));
		butOK = new JButton("OK");
		butCancel = new JButton("Cancel");
		panCtrl.add(butOK);
		panCtrl.add(butCancel);

		Container container = getContentPane();
		container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
		container.add(panProficiency);
		container.add(new JSeparator());
		container.add(panComment);
		container.add(new JSeparator());
		container.add(panCtrl);

		pack();

		sldProficiency.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				int proficiency = sldProficiency.getValue();
				labProficiencyValue.setText("" + proficiency);
			}
		});
		butOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				okButtonPressed(e);
			}
		});
		butCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelButtonPressed(e);
			}
		});
	}

	@Override
	public Insets getInsets() {
		Insets insets = super.getInsets();
		int margin = 10;
		return new Insets(insets.top + margin, insets.left + margin,
				insets.bottom + margin, insets.right + margin);
	}

	private JSlider makeSlider() {
		JSlider result = new JSlider(SwingConstants.HORIZONTAL);

		result = new JSlider(SwingConstants.HORIZONTAL);
		result.setBackground(Color.WHITE);
		result.setPreferredSize(new Dimension(210, 50));
		result.setPaintTicks(true);
		result.setMaximum(UserCompetence.MAX_PROFICIENCY);
		result.setMinimum(UserCompetence.MIN_PROFICIENCY);
		result.setValue(competence.getProficiency());
		result.setMajorTickSpacing(10);
		result.setSnapToTicks(true);
		result.setPaintLabels(true);
		result.setPaintTicks(true);

		return result;
	}

	private void okButtonPressed(ActionEvent e) {
		competence.setProficiency(sldProficiency.getValue());
		competence.setComment(txtComment.getText());
		owner.refresh();
		dispose();
	}

	private void cancelButtonPressed(ActionEvent e) {
		dispose();
	}
}
